package com.skywolf.chem.common.excel;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


public class ExcelRow {

    private Map<String,String> data;
    private  boolean isVerify;

    /**
     * 指示当前行数据是否合法
     * @return
     */
    public boolean isVerify() {
        return isVerify;
    }

    public void setVerify(boolean isVerify) {
        this.isVerify = isVerify;
    }

    public ExcelRow(Map<String, String> data){
        this.data=data;

    }

    public int getCellCount(){
        return  data.size();
    }

    public String getValue(String columnName){
        Integer tmp=getIntValue2(columnName);
        if(tmp!=null){
            return  tmp.toString();
        }
        return data.get(columnName);
    }
    private String getString(String col){
        return data.get(col);
    }

    public Integer getIntValue(String col){
        String v=getString(col);
        if(StringUtils.isEmpty(v))return null;
        try{
            return new Integer(v);
        }catch (Exception e){

        }
        return null;
    }
    public Integer getIntValue2(String col){
        String v=getString(col);
        if(v==null)
            return null;

        int dotIndex=v.indexOf('.');
        if(dotIndex>-1){
            //有小数点
            boolean allZero=true;
            for(int i=dotIndex+1;i<v.length();i++){
                if(v.charAt(i)!='0'){
                    allZero=false;
                    break;
                }
            }
            if(!allZero){
                return null;
            }
            try{
                return new Integer(v.substring(0,dotIndex));
            }catch (Exception e){

            }
            return null;

        }
        return getIntValue(col);
    }

    public BigDecimal getBigDecimal(String col){
        String v=getString(col);
        if(StringUtils.isEmpty(v))return null;
        try{
            return new BigDecimal(v);
        }catch (Exception e){

        }
        return null;
    }

    public void putValue(String columnName, String value){
        data.put(columnName,value);
    }

    /**
     * 解析日期数据
     * @param col
     * @return
     */
    public String getDateString(String col){
        try{
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(HSSFDateUtil.getJavaDate(Double.valueOf(data.get(col))));
        }catch (Exception e){
            return data.get(col);
        }
    }

    @Override
    public String toString() {
        return data.toString();
    }

    public static void main(String[] args) {
        HashMap<String,String >a=new HashMap<>();
        a.put("a","1");
        a.put("b","1.");
        a.put("c","1.0");
        a.put("d","1.1");
        a.put("e", "43219.999988425923");
        ExcelRow row=new ExcelRow(a);
        System.out.println(row.getIntValue2("a"));
        System.out.println(row.getIntValue2("b"));
        System.out.println(row.getIntValue2("c"));
        System.out.println(row.getIntValue2("d"));
        System.out.println(row.getDateString("e"));
    }
}
